package com.ruoyi.web.controller.wechatApplet;

import com.ruoyi.background.domain.AppImg;
import com.ruoyi.background.domain.AppWechatUser;
import com.ruoyi.background.domain.WechatPhone;
import com.ruoyi.background.service.*;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.system.service.ISysConfigService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;


/**
 * 不用登录的接口控制层
 */
@RestController
@RequestMapping("/wechat/out")
public class WechatOutController extends BaseController {

    @Autowired
    private IAppImgService appImgService;

    @Autowired
    private IAppWechatUserService appWechatUserService;

    @Autowired
    private IWechatApiService wechatApiService;


    @Autowired
    private ISysConfigService sysConfigService;

    @Autowired
    private IAppLotteryRecordService appLotteryRecordService;

    @Autowired
    private BuchangService buchangService;


    /**
     * 获取登录的背景图
     *
     * @return 结果
     */
    @GetMapping("/getLoginBg")
    public AjaxResult getLoginBg() {
        AppImg img = appImgService.selectAppImgBySiteIndex("login-bg");
        return AjaxResult.success(img);
    }

    /**
     * 微信授权认证
     *
     * @param code code
     * @return 结果 true需要严重手机号码 false不需要获取手机号码同时返回token
     */
    @GetMapping("/auth/{code}")
    public AjaxResult WechatAuth(@PathVariable String code) {
        AppWechatUser wechatUser = wechatApiService.getUserOpenId(code);
        if (ObjectUtils.isEmpty(wechatUser.getPhoneNumber())) {
            AjaxResult ajaxResult = AjaxResult.success();
            ajaxResult.put("flag", false);
            ajaxResult.put("openid", wechatUser.getOpenid());
            return ajaxResult;
        } else {
            //已经绑定了手机号码 直接登录即可
            return appWechatUserService.weChatUserLogin(wechatUser);
        }
    }

    /**
     * 更新用户的sessionKey
     *
     * @param code code
     * @return 结果
     */
    @GetMapping("/updateSessionKey/{code}")
    public AjaxResult updateSessionKey(@PathVariable String code) {
        return toAjax(wechatApiService.updateSessionKey(code));
    }

    /**
     *
     * @return
     */
    @GetMapping("/create/tokens")
    public AjaxResult createTokens( AppWechatUser wechatUser) {
        startPage();
        return AjaxResult.success(wechatApiService.createTokens(wechatUser));
    }


    /**
     * 获取用户手机号
     *
     * @param wechatPhone 手机号
     * @return 结果
     */
    @PostMapping("/getPhone")
    public AjaxResult getWechatPhone(@RequestBody WechatPhone wechatPhone) {
        AppWechatUser wechatUser = wechatApiService.getUserPhone(wechatPhone);
        //获取成功手机号 进行登录
        return appWechatUserService.weChatUserLogin(wechatUser);
    }


    @GetMapping("/buchang1002")
    public void buchang1002() {
        buchangService.buchang1002();
    }

    @GetMapping("/buchang1010")
    public void buchang1010() {
        buchangService.buchang1010();
    }






}
